<template>
  <div class="container">
    <div class="app-container">
      <!-- 展示树结构 -->
      <el-tree :expand-on-click-node="false" :data="depts" :props="defaultProps" default-expand-all="true">
<!--        节点结构-->
<!--        v-slot="{node,data}",只能作用在template标签里面-->
        <template v-slot="{data}">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{data.label}}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{data.managerName}}</span>
<!--              $event 实参 表示类型-->
              <el-dropdown @command="operateDept($event,data.id)">
                <!--              显示区域-->
                <span class="el-dropdown-link">
                下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
                <!--              选项-->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
<!--    3.组件放在最外层的里一层-->
<!--    表示会接受子组件的事件和值 => 属性-->
    <add-dept :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDeptment="getDepartment"/>
  </div>
</template>

<script>
  import {transListToTreeData} from "@/utils"
  //1.导入组件
  import AddDept from "./components/add-dept.vue"
  export default {
    name: 'Department',
    //2.注册局部组件
    components: {AddDept},
    data() {
      return {
        currentNodeId:null,
        showDialog: false,//控制弹层
        depts: [{
          label: '瑞德', managerName:'管理员',id:1,children: [
            {label: '总裁办', managerName:'历史',id:2},{
              label: '行政部', managerName:'五五',id:3
            },
            {
              label: '人事部', managerName:'全球',id:4
            }
          ]
        }],//数据属性
        defaultProps: {
          label: 'label', //要显示字段的名字
          children: 'children' //读取子节点的字段名
        }
      }
    },
    created() {
      this.getDepartment()
    },
    methods:{
      //封装好的方法
      async getDepartment(){
        // const result = await getDepartment()
        // this.depts = transListToTreeData(result,0)
      },
      //操作部门的方法
      operateDept(type,id){
        if (type==="add"){
          this.showDialog=true
          this.currentNodeId=id
        }
      }

    }
  }
</script>

<!--scoped当前组件生效-->
<style scoped>
  .app-container{
    padding: 30px 140px;
    font-size: 14px;
  }
  .tree-manager{
    width: 50px;
    display: inline-block;
    margin: 10px;
  }
</style>

